//显示加载中进度...
function showLoading(){
	if(!loadingIsShow()){
		//filter:alpha(opacity=60);--兼容ie6、7、8
		var loadingDiv = '<div id="loadingdiv" style="width:100%;height:100%;position:absolute;left:0px;top:0px;z-index:10000;"><div class="loadingdiv" style="width:100%;height:100%;background-color:#ffffff;opacity:0.6;filter:alpha(opacity=60);"></div><img src="../images/loading.gif" alt="加载中。。。" style="position:absolute;top:50%;left:50%;margin-left:-11px;margin-top:-11px;" /></div>';
		$('body').append(loadingDiv);
	}
}
//隐藏加载中进度...
function hideLoading(){
	if(loadingIsShow()){
		$('#loadingdiv').remove();
	}
}
//加载中进度是否已显示
function loadingIsShow(){
	var loadingElement = document.getElementById('loadingdiv');
	if(loadingElement){
		return true;
	}
	return false;
}

//写cookies
function setCookie(name,value,Days)
{
	var exp = new Date();
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	//exp.setTime(exp.getTime() + Days*60*1000);
//	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
	$.cookie(name,value,{expires:exp,path:'/'});
}
//读取cookie
function getCookie(name)
{
//	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
//	if(arr=document.cookie.match(reg))
//	return unescape(arr[2]);
//	else
//	return null;
	var value = $.cookie(name);
	if(!value){
		value = '';
	}
	return value;
}
//删除cookie
function delCookie(name)
{
//	var exp = new Date();
//	exp.setTime(exp.getTime() - 1);
//	var cval=getCookie(name);
//	if(cval!=null)
//	document.cookie= name + "="+cval+";expires="+exp.toGMTString();
	$.cookie(name, '', {path:'/',expires: -1});
}

//JavaScript Document
function textCounter(field, countfield, maxlimit){
	//alert(countfield + " " + maxlimit);
  	//定义函数，传入3个参数，分别为表单区的名字，表单域元素名，字符限制
	if(field.value.length > maxlimit)
  		//如果元素区字符数大于最大字符数，按照最大字符数截断
  		field.value = field.value.substring(0, maxlimit);
  	else
  		//在记数区文本框内显示剩余的字符数
  		countfield.innerHTML = maxlimit - field.value.length;
  		//document.getElementById(countfield).innerHTML = maxlimit - field.value.length;
}


String.prototype.endWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
	  return false;
	if(this.substring(this.length-str.length)==str)
	  return true;
	else
	  return false;
	return true;
}

String.prototype.startWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
	  return false;
	if(this.substr(0,str.length)==str)
	  return true;
	else
	  return false;
	return true;
}

String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim=function(){
	return this.replace(/(^\s*)/g,"");
}
String.prototype.rtrim=function(){
	return this.replace(/(\s*$)/g,"");
}

//获取url中的参数
function getUrlParam(name, search) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    //var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    var r = search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

//baseurl
function getRoot(location) {
    var hostname = location.hostname;
    var pathname = location.pathname;
    var contextPath = pathname.split("/")[1];
    var port = location.port;
    var protocol = location.protocol;
    return protocol + "//" + hostname + ":" + port + "/" + contextPath;
}
//ajax 异步加载数据通用方法
var loadAjaxData =function(argUrl, paramdata, fun,dataType,requestType,async) {
	radomT = "?_=" + (new Date()).getTime();
	var url = argUrl + radomT;
    var params = {

    };
    if(!paramdata){
        paramdata = {

        };
    }
	if(!requestType){
        requestType="POST";
	}
    if(!async){
        async=true;
    }
    if(!dataType){
        dataType="JSON";
    }
	jQuery.ajax({
		url : url,
		type : requestType,
		data : paramdata,
		async : async,
		dataType : dataType,
		contentType : "application/x-www-form-urlencoded;charset=UTF-8",
		complete:function(){
			hideLoading();
		},
		beforeSend : function() {
			showLoading();
		},
		success : function(data) {
			fun(data);
		},
        error:function(){

        }
	});
};
